home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / byacc 1.8.2 / verbose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-04  |  7.4 KB  |  383 lines  |  [TEXT/R*ch]

  1. #include "defs.h"
  2.  
  3.  
  4. static short *null_rules;
  5.  
  6. static void log_unused _P_((void));
  7. static void log_conflicts _P_((void));
  8. static void print_state _P_((int state));
  9. static void print_conflicts _P_((int state));
  10. static void print_core _P_((int state));
  11. static void print_nulls _P_((int state));
  12. static void print_actions _P_((int stateno));
  13. static void print_shifts _P_((register action *p));
  14. static void print_reductions _P_((register action *p, register int defred));
  15. static void print_gotos _P_((int stateno));
  16.  
  17.  
  18. #if __STDC__
  19. void verbose(void)
  20. #else
  21. void verbose()
  22. #endif
  23. {
  24.     register int i;
  25.  
  26.     if (!vflag) return;
  27.  
  28.     null_rules = (short *) MALLOC(nrules*sizeof(short));
  29.     fprintf(verbose_file, "\f\n");
  30.     for (i = 0; i < nstates; i++)
  31.     print_state(i);
  32.     FREE(null_rules);
  33.  
  34.     if (nunused)
  35.     log_unused();
  36.     if (SRtotal || RRtotal)
  37.     log_conflicts();
  38.  
  39.     fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
  40.         nvars);
  41.     fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
  42. }
  43.  
  44.  
  45. #if __STDC__
  46. static void log_unused(void)
  47. #else
  48. static void log_unused()
  49. #endif
  50. {
  51.     register int i;
  52.     register short *p;
  53.  
  54.     fprintf(verbose_file, "\n\nRules never reduced:\n");
  55.     for (i = 3; i < nrules; ++i)
  56.     {
  57.     if (!rules_used[i])
  58.     {
  59.         fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
  60.         for (p = ritem + rrhs[i]; *p >= 0; ++p)
  61.         fprintf(verbose_file, " %s", symbol_name[*p]);
  62.         fprintf(verbose_file, "  (%d)\n", i - 2);
  63.     }
  64.     }
  65. }
  66.  
  67.  
  68. #if __STDC__
  69. static void log_conflicts(void)
  70. #else
  71. static void log_conflicts()
  72. #endif
  73. {
  74.     register int i;
  75.  
  76.     fprintf(verbose_file, "\n\n");
  77.     for (i = 0; i < nstates; i++)
  78.     {
  79.     if (SRconflicts[i] || RRconflicts[i])
  80.     {
  81.         fprintf(verbose_file, "State %d contains ", i);
  82.         if (SRconflicts[i] == 1)
  83.         fprintf(verbose_file, "1 shift/reduce conflict");
  84.         else if (SRconflicts[i] > 1)
  85.         fprintf(verbose_file, "%d shift/reduce conflicts",
  86.             SRconflicts[i]);
  87.         if (SRconflicts[i] && RRconflicts[i])
  88.         fprintf(verbose_file, ", ");
  89.         if (RRconflicts[i] == 1)
  90.         fprintf(verbose_file, "1 reduce/reduce conflict");
  91.         else if (RRconflicts[i] > 1)
  92.         fprintf(verbose_file, "%d reduce/reduce conflicts",
  93.             RRconflicts[i]);
  94.         fprintf(verbose_file, ".\n");
  95.     }
  96.     }
  97. }
  98.  
  99.  
  100. #if __STDC__
  101. static void print_state(int state)
  102. #else
  103. static void print_state(state)
  104. int state;
  105. #endif
  106. {
  107.     if (state)
  108.     fprintf(verbose_file, "\n\n");
  109.     if (SRconflicts[state] || RRconflicts[state])
  110.     print_conflicts(state);
  111.     fprintf(verbose_file, "state %d\n", state);
  112.     print_core(state);
  113.     print_nulls(state);
  114.     print_actions(state);
  115. }
  116.  
  117.  
  118. #if __STDC__
  119. static void print_conflicts(int state)
  120. #else
  121. static void print_conflicts(state)
  122. int state;
  123. #endif
  124. {
  125.     register int symbol, act, number;
  126.     register action *p;
  127.  
  128.     symbol = -1;
  129.     for (p = parser[state]; p; p = p->next)
  130.     {
  131.     if (p->suppressed == 2)
  132.         continue;
  133.  
  134.     if (p->symbol != symbol)
  135.     {
  136.         symbol = p->symbol;
  137.         number = p->number;
  138.         if (p->action_code == SHIFT)
  139.         act = SHIFT;
  140.         else
  141.         act = REDUCE;
  142.     }
  143.     else if (p->suppressed == 1)
  144.     {
  145.         if (state == final_state && symbol == 0)
  146.         {
  147.         fprintf(verbose_file, "%d: shift/reduce conflict \
  148. (accept, reduce %d) on $end\n", state, p->number - 2);
  149.         }
  150.         else
  151.         {
  152.         if (act == SHIFT)
  153.         {
  154.             fprintf(verbose_file, "%d: shift/reduce conflict \
  155. (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
  156.                 symbol_name[symbol]);
  157.         }
  158.         else
  159.         {
  160.             fprintf(verbose_file, "%d: reduce/reduce conflict \
  161. (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
  162.                 symbol_name[symbol]);
  163.         }
  164.         }
  165.     }
  166.     }
  167. }
  168.  
  169.  
  170. #if __STDC__
  171. static void print_core(int state)
  172. #else
  173. static void print_core(state)
  174. int state;
  175. #endif
  176. {
  177.     register int i;
  178.     register int k;
  179.     register int rule;
  180.     register core *statep;
  181.     register short *sp;
  182.     register short *sp1;
  183.  
  184.     statep = state_table[state];
  185.     k = statep->nitems;
  186.  
  187.     for (i = 0; i < k; i++)
  188.     {
  189.     sp1 = sp = ritem + statep->items[i];
  190.  
  191.     while (*sp >= 0) ++sp;
  192.     rule = -(*sp);
  193.     fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
  194.  
  195.     for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  196.         fprintf(verbose_file, "%s ", symbol_name[*sp]);
  197.  
  198.     putc('.', verbose_file);
  199.  
  200.     while (*sp >= 0)
  201.     {
  202.         fprintf(verbose_file, " %s", symbol_name[*sp]);
  203.         sp++;
  204.     }
  205.     fprintf(verbose_file, "  (%d)\n", -2 - *sp);
  206.     }
  207. }
  208.  
  209.  
  210. #if __STDC__
  211. static void print_nulls(int state)
  212. #else
  213. static void print_nulls(state)
  214. int state;
  215. #endif
  216. {
  217.     register action *p;
  218.     register int i, j, k, nnulls;
  219.  
  220.     nnulls = 0;
  221.     for (p = parser[state]; p; p = p->next)
  222.     {
  223.     if (p->action_code == REDUCE &&
  224.         (p->suppressed == 0 || p->suppressed == 1))
  225.     {
  226.         i = p->number;
  227.         if (rrhs[i] + 1 == rrhs[i+1])
  228.         {
  229.         for (j = 0; j < nnulls && i > null_rules[j]; ++j)
  230.             continue;
  231.  
  232.         if (j == nnulls)
  233.         {
  234.             ++nnulls;
  235.             null_rules[j] = i;
  236.         }
  237.         else if (i != null_rules[j])
  238.         {
  239.             ++nnulls;
  240.             for (k = nnulls - 1; k > j; --k)
  241.             null_rules[k] = null_rules[k-1];
  242.             null_rules[j] = i;
  243.         }
  244.         }
  245.     }
  246.     }
  247.  
  248.     for (i = 0; i < nnulls; ++i)
  249.     {
  250.     j = null_rules[i];
  251.     fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
  252.         j - 2);
  253.     }
  254.     fprintf(verbose_file, "\n");
  255. }
  256.  
  257.  
  258. #if __STDC__
  259. static void print_actions(int stateno)
  260. #else
  261. static void print_actions(stateno)
  262. int stateno;
  263. #endif
  264. {
  265.     register action *p;
  266.     register shifts *sp;
  267.     register int as;
  268.  
  269.     if (stateno == final_state)
  270.     fprintf(verbose_file, "\t$end  accept\n");
  271.  
  272.     p = parser[stateno];
  273.     if (p)
  274.     {
  275.     print_shifts(p);
  276.     print_reductions(p, defred[stateno]);
  277.     }
  278.  
  279.     sp = shift_table[stateno];
  280.     if (sp && sp->nshifts > 0)
  281.     {
  282.     as = accessing_symbol[sp->shift[sp->nshifts - 1]];
  283.     if (ISVAR(as))
  284.         print_gotos(stateno);
  285.     }
  286. }
  287.  
  288.  
  289. #if __STDC__
  290. static void print_shifts(register action *p)
  291. #else
  292. static void print_shifts(p)
  293. register action *p;
  294. #endif
  295. {
  296.     register int count;
  297.     register action *q;
  298.  
  299.     count = 0;
  300.     for (q = p; q; q = q->next)
  301.     {
  302.     if (q->suppressed < 2 && q->action_code == SHIFT)
  303.         ++count;
  304.     }
  305.  
  306.     if (count > 0)
  307.     {
  308.     for (; p; p = p->next)
  309.     {
  310.         if (p->action_code == SHIFT && p->suppressed == 0)
  311.         fprintf(verbose_file, "\t%s  shift %d\n",
  312.                 symbol_name[p->symbol], p->number);
  313.     }
  314.     }
  315. }
  316.  
  317.  
  318. #if __STDC__
  319. static void print_reductions(register action *p, register int defred)
  320. #else
  321. static void print_reductions(p, defred)
  322. register action *p;
  323. register int defred;
  324. #endif
  325. {
  326.     register int k, anyreds;
  327.     register action *q;
  328.  
  329.     anyreds = 0;
  330.     for (q = p; q ; q = q->next)
  331.     {
  332.     if (q->action_code == REDUCE && q->suppressed < 2)
  333.     {
  334.         anyreds = 1;
  335.         break;
  336.     }
  337.     }
  338.  
  339.     if (anyreds == 0)
  340.     fprintf(verbose_file, "\t.  error\n");
  341.     else
  342.     {
  343.     for (; p; p = p->next)
  344.     {
  345.         if (p->action_code == REDUCE && p->number != defred)
  346.         {
  347.         k = p->number - 2;
  348.         if (p->suppressed == 0)
  349.             fprintf(verbose_file, "\t%s  reduce %d\n",
  350.                 symbol_name[p->symbol], k);
  351.         }
  352.     }
  353.  
  354.     if (defred > 0)
  355.         fprintf(verbose_file, "\t.  reduce %d\n", defred - 2);
  356.     }
  357. }
  358.  
  359.  
  360. #if __STDC__
  361. static void print_gotos(int stateno)
  362. #else
  363. static void print_gotos(stateno)
  364. int stateno;
  365. #endif
  366. {
  367.     register int i, k;
  368.     register int as;
  369.     register short *to_state;
  370.     register shifts *sp;
  371.  
  372.     putc('\n', verbose_file);
  373.     sp = shift_table[stateno];
  374.     to_state = sp->shift;
  375.     for (i = 0; i < sp->nshifts; ++i)
  376.     {
  377.     k = to_state[i];
  378.     as = accessing_symbol[k];
  379.     if (ISVAR(as))
  380.         fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
  381.     }
  382. }
  383.